/**************************************
name:		kayla freeman
date:		8/1/24
purpose:	addressing selection using auditor instrument 
**************************************/

clear
use 04_prepped_data

ren s_gvkey gvkey
merge m:1 gvkey year using 13_Auds /*has gvkey, year, rank, au, from Compustat*/
ren au s_au
ren rank s_aurank
ren gvkey s_gvkey
drop if _merge==2 
drop _merge

destring s_gvkey c_gvkey, replace
drop if na=="missing" /*10ks not available in edgar*/
gen yes = na=="yes"


gen prop_sale = csale/s_revt
gen ln_prop = ln(prop_sale*100)
label var prop_sale "share of supplier's sales"
label var ln_prop "Sales dependence"

*fill in ar_amount & ar_perc
replace ar_amount = ar_amount/1000000
replace ar_amount = ar_perc*s_rect if missing(ar_amount)
replace ar_perc = ar_amount/s_rect if missing(ar_perc)

*relationship length
gen rel_length = ln(1+year-scyr1)


*sp_arS
gen sp_arS = ar_amount/csale

ren s_lnage s_age
ren c_lnage c_age

egen cs_id = group(s_id c_id)

keep if  year<2017

keep if !missing(s_hhi, s_mkt_share, s_lnat, s_blev, s_profit, c_hhi, c_mkt_share, c_lnat, c_blev, c_profit,  prop_sale)


winsor2 ar_perc  s_hhi s_mkt_share s_ap_at s_ar_at s_ap_cogs s_ar_revt s_lnat s_q s_tang  s_profit c_hhi c_mkt_share c_ap_at c_ar_at c_ap_cogs s_age c_age ///
	c_ar_revt c_lnat c_q c_tang  c_profit prop_sale ln_prop rel_length    sp_arS   s_rdat c_rdat s_curdat c_curdat,replace

replace s_blev = 1 if s_blev>1
replace c_blev = 1 if c_blev>1
replace s_blev = 0 if s_blev<0
replace c_blev = 0 if c_blev<0


label var s_lnat "S Size"
label var c_lnat "C Size"
label var s_profit "S Profitability"
label var c_profit "C Profitability"
label var s_hhi "S HHI"
label var c_hhi "C HHI"
label var rel_length "Relationship Length"
label var sp_arS "Trade Credit"
label var ln_prop "Sales Dependence"
label var s_blev "S Leverage"
label var c_blev "C Leverage"



 sort s_id year sp_arS  
  capture drop first
 by s_id year: gen first = _n==1
 tab s_au if first==1

 tab first yes
/*
TABLE IA-1: COMPARISON OF REP AND NON-REP
*/
capture drop meansalesdep
bys s_gvkey year: egen meansalesdep = mean(ln_prop)
label var meansalesdep "Sales Dependence"
foreach var of varlist meansalesdep s_age s_hhi s_ar_revt s_lnat s_blev s_profit{
di "`: var label `var''"
	ttest `var',by(yes) ,if first==1
}
tempfile audready
save `audready'
/**********************
INTERLUDE FOR PREPPING THE PERCAUD VAR; 

	note: this takes some time
**********************/

levelsof s_id, local(levels)
foreach lev of local levels{
	preserve
gen supper = `lev'
keep if first==1
drop if s_id==`lev' /*whatever the observation supplier is, drop it and focus on others*/
gen repped = !missing(sp_arS)
drop if s_au==0 | s_au==9 /*unaudited==0 and other==9*/
bysort s_au year: gen audnum = _N /*count how many suppliers in a year use each auditor*/
bysort s_au year: egen audrep = total(repped) /*count within each auditor year how many supps reported tc*/
keep  s_au year audnum audrep supper
bys s_au year: keep if _n==1 /*keep one obs per auditor-year*/
sort s_au year
by s_au: gen cumaudnum = audnum[_n-1] if year[_n-1]>=year-5 &_n>1
by s_au: gen cumaudrep = audrep[_n-1] if year[_n-1]>=year-5 & _n>1
forval i = 2/5{
by s_au: replace cumaudnum = cumaudnum + audnum[_n-`i'] if year[_n-`i']>=year-5 & _n>`i' &!missing(audnum[_n-`i'])  
by s_au: replace cumaudrep = cumaudrep + audrep[_n-`i'] if year[_n-`i']>=year-5& _n>`i' &!missing(audrep[_n-`i']) 
}
gen audperc = cumaudrep/cumaudnum
replace audperc = . if cumaudnum<10
keep s_au year audperc supper cumaudnum
ren supper s_id
tempfile `lev'it
save ``lev'it'
restore
}
clear

foreach lev of local levels{
append using ``lev'it'
}
sort s_au year /*gives us a file that has running audperc for each supplier-year combo for all auds. */
drop if missing(audperc)
tab cumaudnum
tempfile runningauds
save `runningauds'

clear
use `audready'
merge m:1 s_id s_au year using `runningauds'
drop if _merge==2
drop _merge

sum audperc,det
capture drop hiaud

egen hiaud = xtile(audperc),by(year) n(2)
	replace hiaud = 0 if hiaud==1
	replace hiaud=1 if hiaud==2
	

sort s_au year s_id
order s_id year s_au audperc yes
global svars   s_lnat s_blev s_profit s_hhi  s_age  
global cvars   c_lnat  c_blev c_profit c_hhi   c_age
global relvars ln_prop  
global clustvar s_id  c_id


gen cons =1
egen audyr = group(s_au year) 
capture drop use
gen use = 1
/*
TABLE IA-3, PANEL A: heckman selection test, plus using lambda in stricter fe models
*/
capture drop lambda
heckman sp_arS  $relvars   $svars $cvars  i.s_ind i.c_ind , select(yes = audperc $relvars $svars $cvars i.s_ind i.c_ind ) twostep  mills(lambda) ,if use==1
 eststo heckit, title(Trade Credit)
eststo heckitsel,title(Selection)
qui estadd local twostep "Yes"
reghdfe sp_arS $relvars $svars $cvars lambda, absorb(cons) vce(cluster $clustvar ),if use==1
reghdfe sp_arS $relvars $svars $cvars lambda, absorb(s_id c_id year) vce(cluster $clustvar ),if use==1
eststo firms, title(Trade Credit)
qui estadd local sfe "Yes"
qui estadd local cfe "Yes"
qui estadd local yfe "Yes"
reghdfe sp_arS $relvars $svars $cvars lambda, absorb(cs_id year) vce(cluster $clustvar ),if use==1
eststo csalt,title(Trade Credit)
qui estadd local pair "Yes"
qui estadd local yfe "Yes"
 estout  heckitsel heckit  firms csalt, drop(*s_ind *c_ind)  order(ln_prop audperc) cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) label

# delimit ;
esttab  heckitsel heckit  firms csalt   , append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  order(ln_prop audperc) stats(twostep sfe cfe yfe r2_a N, fmt(%20s %20s %20s %20s %9.3fc %9.0fc)label("Two-Step" "Supplier FE" "Customer FE" "Year FE" "R2" "Observations"))  label 
	title("Heckman Correction for Reporting Selection")  collabels(none)   nomtitles
	 drop(*s_ind *c_ind _cons) 
;
#delimit cr


preserve
sort s_gvkey year
	gen yesyr = year if yes==1
bys s_gvkey: egen minyear = min(yesyr)
bys s_gvkey: egen overallmin = min(year)
gen newyes = 0
	replace newyes = 1 if year>=minyear & year<minyear+3
tab newyes
bys s_gvkey year: keep if _n==1
replace newyes = 0 if minyear==overallmin
tab newyes
keep s_gvkey year newyes
tempfile yessy
save `yessy'

restore
merge m:1 s_gvkey year using `yessy' ,keepusing(newyes)
	drop _m

bys s_gvkey: egen firstyr = min(year)

capture drop use
gen use = newyes==1 &year>firstyr
	replace use = . if s_au==9

save 14_prematch,replace

/*BELOW: MATCHING AND E-BALANCING FOR TABLES IA3, PANELS B AND C, AND IA4*/

drop if year<1996 /*spotty before this*/
drop if s_au==9 | missing(s_au) | missing(audperc) /*other category plus other filters*/
keep if !missing(s_lnat ,s_blev ,s_profit, s_hhi ,ln_prop)

collapse (sum) prop_sale (mean) sp_arS s_age s_lnat s_blev s_profit s_hhi ln_prop hiaud s_ar_revt c_age c_lnat c_blev c_profit c_hhi (max) yes ,by(s_gvkey c_gvkey  year)
keep if yes==1

global matchy s_lnat s_blev s_profit s_hhi ln_prop  s_age c_lnat c_blev c_profit c_hhi c_age

teffects psmatch (sp_arS) (hiaud $matchy ), gen(match) caliper(.01) osample(bad) nneighbor(1) /*there will be nonmatches... repeat next 3 lines, dropping those without matches until matching achieved for all remaining*/
drop if bad
drop bad
teffects psmatch (sp_arS) (hiaud $matchy ), gen(match) caliper(.01) osample(bad) nneighbor(1)

tebalance summarize

gen ob = _n
tempfile tempy
save `tempy'

keep if hiaud==1
keep match1
bys match1: gen mweight = _N
	tab mweight
by match1: keep if _n==1
ren match1 ob

keep ob mweight
merge 1:1 ob using `tempy'
replace mweight = 1 if hiaud==1
keep s_gvk c_gvk mweight hiaud year
drop if missing(mweight) 
bys s_gvkey c_gvkey year: keep if _n==_N

tempfile matcher
save `matcher'

clear 
use 14_prematch

merge 1:m s_gvkey c_gvkey year using `matcher',keepusing(mweight)
	keep if _m==3
	drop _m

	expand mweight
	capture drop sfirst
	bys s_gvkey year: gen sfirst = _n==1
	
/*comparison of vars for Table IA-4, panel A, cols 1 and 2*/
	foreach var of varlist $svars  $cvars ln_prop {
		di `"`: var label `var''"' 
		ttest `var' ,by(hiaud) ,
		}
reghdfe sp_arS  c.ln_prop#i.hiaud   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ), 
	est sto intx1
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 
		 test c.ln_prop#0.hiaud ==c.ln_prop#1.hiaud
		 
reghdfe sp_arS  c.ln_prop#i.hiaud   $svars $cvars , absorb(year cs_id  ) vce(cluster $clustvar ), 
	est sto intx2
 		 quietly estadd local csfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 test c.ln_prop#0.hiaud ==c.ln_prop#1.hiaud
		 
	estout intx1 intx2 , label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

	
	
/*Table IA-4, cols 1-2*/
# delimit ;
esttab  intx1  intx2     , append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  order(ln_prop ) stats( sfe cfe yfe r2_a N, fmt( %20s %20s %20s %9.3fc %9.0fc)label("Supplier FE" "Customer FE" "Year FE" "R2" "Observations"))  label 
	title("Propensity Score Matching on Auditor Propensity")  collabels(none)   nomtitles
	 drop( _cons) 
;
#delimit cr

/*BELOW: MATCHING AND E-BALANCING FOR TABLES IA3, PANELS B AND C, AND IA4*/
clear
use 14_prematch
keep if newyes==1
drop if year<1996 /*spotty before this*/
drop if s_au==9 | missing(s_au) | missing(audperc) /*other category plus other filters*/
keep if !missing(s_lnat ,s_blev ,s_profit, s_hhi ,ln_prop)

collapse (sum) prop_sale (mean) sp_arS s_age s_lnat s_blev s_profit s_hhi ln_prop hiaud s_ar_revt c_age c_lnat c_blev c_profit c_hhi (max) yes ,by(s_gvkey c_gvkey  year)
keep if yes==1

global matchy s_lnat s_blev s_profit s_hhi ln_prop  s_age c_lnat c_blev c_profit c_hhi c_age

teffects psmatch (sp_arS) (hiaud $matchy ), gen(match) caliper(.01) osample(bad) nneighbor(1) /*there will be nonmatches... repeat next 3 lines, dropping those without matches until matching achieved for all remaining*/
drop if bad
drop bad
teffects psmatch (sp_arS) (hiaud $matchy ), gen(match) caliper(.01) osample(bad) nneighbor(1)

tebalance summarize

gen ob = _n
tempfile tempy
save `tempy'

keep if hiaud==1
keep match1
bys match1: gen mweight = _N
	tab mweight
by match1: keep if _n==1
ren match1 ob

keep ob mweight
merge 1:1 ob using `tempy'
replace mweight = 1 if hiaud==1
keep s_gvk c_gvk mweight hiaud year
drop if missing(mweight) 
bys s_gvkey c_gvkey year: keep if _n==_N

tempfile matcher
save `matcher'

clear 
use 14_prematch

merge 1:m s_gvkey c_gvkey year using `matcher',keepusing(mweight)
	keep if _m==3
	drop _m

	expand mweight
	capture drop sfirst
	bys s_gvkey year: gen sfirst = _n==1
	
/*comparison of vars for Table IA-4, panel A, cols 3 and 4*/
	foreach var of varlist $svars  $cvars ln_prop {
		di `"`: var label `var''"' 
		ttest `var' ,by(hiaud) , 
		}
reghdfe sp_arS  c.ln_prop#i.hiaud   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ), 
	est sto intx1
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 
		 test c.ln_prop#0.hiaud ==c.ln_prop#1.hiaud
		 
reghdfe sp_arS  c.ln_prop#i.hiaud   $svars $cvars , absorb(year cs_id  ) vce(cluster $clustvar ), 
	est sto intx2
 		 quietly estadd local csfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 test c.ln_prop#0.hiaud ==c.ln_prop#1.hiaud
		 
	estout intx1 intx2 , label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

	
	
/*Table IA-4, cols 3-4*/
# delimit ;
esttab  intx1  intx2     , append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  order(ln_prop ) stats( sfe cfe yfe r2_a N, fmt( %20s %20s %20s %9.3fc %9.0fc)label("Supplier FE" "Customer FE" "Year FE" "R2" "Observations"))  label 
	title("Propensity Score Matching on Auditor Propensity")  collabels(none)   nomtitles
	 drop( _cons) 
;
#delimit cr


/*E-BALANCING*/
clear 
use 14_prematch

drop if year<1996
drop if s_au==9 | missing(s_au) | missing(audperc) /*other category plus other filters*/
keep if !missing(s_lnat ,s_blev ,s_profit, s_hhi ,ln_prop)
capture drop _merge
keep if yes==1
collapse (sum) prop_sale (mean) sp_arS s_age s_lnat s_blev s_profit s_hhi ln_prop hiaud s_ar_revt c_age c_lnat c_blev c_profit c_hhi (max) yes ,by(s_gvkey c_gvkey  year)

gen balvar = hiaud
global matchy s_lnat s_blev s_profit s_hhi ln_prop  s_age c_lnat c_blev c_profit c_hhi c_age
/*below gives results of balancing for Panel B of IA-4*/
ebalance balvar $matchy , maxiter(500) tar(2) tolerance(.01) gen(eweight) 
keep s_gvkey c_gvkey year hiaud eweight 

merge 1:m s_gvkey c_gvkey year    using 14_prematch

reghdfe sp_arS  c.ln_prop#i.hiaud   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   [pweight=eweight] if !missing(eweight)
	est sto intx1ag
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 test c.ln_prop#0.hiaud ==c.ln_prop#1.hiaud

		 reghdfe sp_arS  c.ln_prop#i.hiaud   $svars $cvars , absorb(year cs_id  ) vce(cluster $clustvar ),   [pweight=eweight] if  !missing(eweight) 
	est sto intx2ag
 		 quietly estadd local csfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 test c.ln_prop#0.hiaud ==c.ln_prop#1.hiaud
	
estout intx1ag intx2ag , order( ln_prop  ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

/*Table IA-3 Panel C, cols 1-2*/
# delimit ;
esttab  intx1  intx2     , append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  order(ln_prop ) stats( sfe cfe yfe r2_a N, fmt( %20s %20s %20s %9.3fc %9.0fc)label("Supplier FE" "Customer FE" "Year FE" "R2" "Observations"))  label 
	title("Entropy Balancing on Auditor Propensity")  collabels(none)   nomtitles
	 drop( _cons) 
;
#delimit cr


/*E-BALANCING*/
clear 
use 14_prematch

drop if year<1996
drop if s_au==9 | missing(s_au) | missing(audperc) /*other category plus other filters*/
keep if newyes==1
keep if !missing(s_lnat ,s_blev ,s_profit, s_hhi ,ln_prop)
capture drop _merge
keep if yes==1
collapse (sum) prop_sale (mean) sp_arS s_age s_lnat s_blev s_profit s_hhi ln_prop hiaud s_ar_revt c_age c_lnat c_blev c_profit c_hhi (max) yes ,by(s_gvkey c_gvkey  year)

gen balvar = hiaud
global matchy s_lnat s_blev s_profit s_hhi ln_prop  s_age c_lnat c_blev c_profit c_hhi c_age
/*below gives results of balancing for Panel B of IA-4*/
ebalance balvar $matchy , maxiter(500) tar(2) tolerance(.01) gen(eweight) 
keep s_gvkey c_gvkey year hiaud eweight 

merge 1:m s_gvkey c_gvkey year    using 14_prematch

reghdfe sp_arS  c.ln_prop#i.hiaud   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   [pweight=eweight] if !missing(eweight)
	est sto intx1ag
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 test c.ln_prop#0.hiaud ==c.ln_prop#1.hiaud

		 reghdfe sp_arS  c.ln_prop#i.hiaud   $svars $cvars , absorb(year cs_id  ) vce(cluster $clustvar ),   [pweight=eweight] if  !missing(eweight) 
	est sto intx2ag
 		 quietly estadd local csfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 test c.ln_prop#0.hiaud ==c.ln_prop#1.hiaud
	
estout intx1ag intx2ag , order( ln_prop  ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 


/*Table IA-3 Panel C, cols 3-4*/
# delimit ;
esttab  intx1  intx2     , append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  order(ln_prop ) stats( sfe cfe yfe r2_a N, fmt( %20s %20s %20s %9.3fc %9.0fc)label("Supplier FE" "Customer FE" "Year FE" "R2" "Observations"))  label 
	title("Entropy Balancing  on Auditor Propensity")  collabels(none)   nomtitles
	 drop( _cons) 
;
#delimit cr


